package unittest;

import junit.framework.TestCase;
import project5.Document;
import project5.DocumentLoader;
import project5.ErrorUtility;
import project5.ProjectException;
import java.util.List;
import java.util.ArrayList;

public class DocumentLoaderTest extends TestCase
{
	public static String DOC_FILE = "data/loaderTest.txt";
	
	public void testLoadDocument()
	{
		DocumentLoader loader = new DocumentLoader();
		
		//Test basic loader
		try
		{
			Document doc = loader.loadDocument(DOC_FILE);
			assertEquals(doc.getFilename(), "loaderTest.txt");
			assertEquals(doc.getSize(), 51);
			assertEquals(doc.getWordCount(), 10);
			assertEquals(doc.getLineCount(), 4);
		}
		catch (ProjectException pe)
		{
			ErrorUtility.handleError(pe);
			assertTrue(false);
		}
		
		//Test with break characters
		try
		{
			loader = new DocumentLoader();
			loader.setBreakCharacters("i");
			
			Document doc = loader.loadDocument(DOC_FILE);
			
			assertEquals(doc.getFilename(), "loaderTest.txt");
			assertEquals(doc.getSize(), 51);
			assertEquals(doc.getWordCount(), 13);
			assertEquals(doc.getLineCount(), 4);
		}
		catch (ProjectException pe)
		{
			ErrorUtility.handleError(pe);
			assertTrue(false);
		}
		
		//Test with stop words
		try
		{
			loader = new DocumentLoader();
			
			List<String> stopWords = new ArrayList<String>();
			stopWords.add("This");
			stopWords.add("cookie");
			
			loader.setStopWords(stopWords);
			
			Document doc = loader.loadDocument(DOC_FILE);
			
			assertEquals(doc.getFilename(), "loaderTest.txt");
			assertEquals(doc.getSize(), 51);
			assertEquals(doc.getWordCount(), 7);
			assertEquals(doc.getLineCount(), 4);
		}
		catch (ProjectException pe)
		{
			ErrorUtility.handleError(pe);
			assertTrue(false);
		}
		
		//Test with both
		try
		{
			loader = new DocumentLoader();
			loader.setBreakCharacters("o");
			
			List<String> stopWords = new ArrayList<String>();
			stopWords.add("is");
			
			loader.setStopWords(stopWords);
			
			Document doc = loader.loadDocument(DOC_FILE);
			
			assertEquals(doc.getFilename(), "loaderTest.txt");
			assertEquals(doc.getSize(), 51);
			assertEquals(doc.getWordCount(), 10);
			assertEquals(doc.getLineCount(), 4);
		}
		catch (ProjectException pe)
		{
			ErrorUtility.handleError(pe);
			assertTrue(false);
		}
	}
}
